home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0045_Definitive Assert in Delphi.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  2.1 KB  |  65 lines

  1.  
  2. The definitive Assert.  Better than the line numbers supplied w/ the
  3. C/C++ Assert, this assert gives you the "find|error" logical address
  4. of the calling module that called assert.  It returns the ssss:oooo
  5. seg offset pair so you can find where the assert was called.  Again,
  6. you can use the ifopt D+ if you wish but I prefer a define _NDEBUG
  7. instead.  Hopefully this is the end-all assert. Use freely, just drop
  8. me an e-mail at vandewb@wku.wkuvx1.edu if you find it useful.
  9.  
  10. Here it is...Jay Cole
  11.  
  12. { Call if you need to verify implied conditions }
  13. procedure Assert(assertedCond : boolean; const msgStr : string);
  14.  
  15. implementation
  16. {$ifdef WINDOWS}
  17.    uses WinProcs, SysUtils, WinTypes;
  18. {$else}
  19.    uses sysUtils;
  20. {$endif}
  21.  
  22. { Error when the assumption made is not correct. }
  23. procedure Assert(assertedCond : boolean; const msgStr : string);
  24. var 
  25.    msgOut : array[0..300] of char;
  26.    hexStr : array[0..30] of char;
  27.    progSeg, progOfs : word;
  28.    tmpStr : string;
  29. begin
  30.    { Get the logical segment used by the Find|Error menu option of
  31. delphi }
  32.    asm
  33.       mov ax, [bp+04]            { Load physical segm of the calling
  34. proc on call stack }
  35.       mov es, ax
  36.       mov ax, word ptr es:0      { Logical segm stored in 0th position
  37. of physical seg }
  38.       mov progSeg, ax            { Logical, not physical segm used in
  39. Find|Error menu item }
  40.       mov ax, [bp+02]
  41.       mov progOfs, ax            { Physical ofs is used by find|error,
  42. no translation needed }
  43.    end;
  44.  
  45.    {$ifndef _NDEBUG} { Are we allowed to assert? }
  46.    if (not assertedCond) then begin
  47.       { construct msg\nat location ssss:oooo using the logical address
  48. }
  49.       StrPCopy(msgOut, msgStr);
  50.       tmpStr := chr(10)+'at location
  51. '+IntToHex(progSeg,4)+':'+IntToHex(progOfs,4);
  52.       StrPCopy(hexStr, tmpStr);
  53.       StrCat(msgOut, hexStr);
  54.       {$ifdef WINDOWS}
  55.          MessageBox(0, msgOut, 'Assert Failed',
  56. MB_ICONSTOP+MB_SYSTEMMODAL+MB_OK);
  57.       {$else}
  58.          WriteLn('Assert Failed', msgOut);
  59.       {$endif}
  60.       { Now, terminate the program because of assertion problem }
  61.       halt; 
  62.    end;
  63.    {$endif}
  64. end;
  65.